package com.youan.serve.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youan.pangu.security.bean.LoginInfor;
import com.youan.serve.controller.swagger.EmailControllerSwagger;
import com.youan.serve.service.IEmailService;
import com.youan.serve.dao.domain.Email;
import com.youan.pangu.base.util.PrimaryKeyUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-10
 */
@Controller
@RequestMapping("/email")
public class EmailController implements EmailControllerSwagger{
    @Autowired
    private IEmailService emailService;

    @Override
    @PostMapping("/email")
    public Email addEmail(Email email, LoginInfor loginInfor){
        email.setEmailId(PrimaryKeyUtil.getCommonId());
        email.setSendUserId(loginInfor.getUserId());
        email.setSendDate(new Date());
        emailService.save(email);
        return email;
    }

    @Override
    @DeleteMapping("/email/{emailId}")
    public void deleteEmailByEmailId(@PathVariable String emailId){
        emailService.removeById(emailId);
    }

    @Override
    @GetMapping("/email/{emailId}")
    public Email selectEmailByEmailId(@PathVariable String emailId){
        return emailService.getById(emailId);
    }

    @Override
    @PutMapping("/email")
    public Email updateEmail(Email email){
        emailService.updateById(email);
        return email;
    }

    @Override
    @GetMapping("/emails")
    public List<Email> selectAllEmail(){
        return emailService.list();
    }

    @Override
    @GetMapping("/sendEmails")
    public List<Email> selectEmailsBySendUserId(LoginInfor loginInfor) throws Exception{
        List<Email> emailList = emailService.list(new LambdaQueryWrapper<Email>().eq(Email::getSendUserId, loginInfor.getUserId()));
        return emailService.setName(emailList);
    }

    @Override
    @GetMapping("/receiveEmails")
    public List<Email> selectEmailsByReceiveUserId(LoginInfor loginInfor) throws Exception{
        List<Email> emailList = emailService.list(new LambdaQueryWrapper<Email>().eq(Email::getReceiveUserId, loginInfor.getUserId()).ne(Email::getIsDelete, "1"));
        List<Email> emailList2 = emailService.list(new LambdaQueryWrapper<Email>().isNull(Email::getIsDelete).eq(Email::getReceiveUserId, loginInfor.getUserId()));
        emailList.addAll(emailList2);
        return emailService.setName(emailList);
    }

    @Override
    @GetMapping("/deletedEmails")
    public List<Email> selectEmailsByReceiveUserIdWithDeleted(LoginInfor loginInfor) throws Exception{
        List<Email> emailList = emailService.list(new LambdaQueryWrapper<Email>().eq(Email::getReceiveUserId, loginInfor.getUserId()).eq(Email::getIsDelete, "1"));
        return emailService.setName(emailList);
    }

    @GetMapping("/receivedAndNotReadEmails")
    public List<Email> selectEmailsByReceiveUserIdWithNotRead(LoginInfor loginInfor) throws Exception{
        List<Email> emailList = emailService.list(new LambdaQueryWrapper<Email>().eq(Email::getReceiveUserId, loginInfor.getUserId()).ne(Email::getIsRead, "1"));
        List<Email> emailList2 = emailService.list(new LambdaQueryWrapper<Email>().isNull(Email::getIsRead).eq(Email::getReceiveUserId, loginInfor.getUserId()));
        emailList.addAll(emailList2);
        return emailService.setName(emailList);
    }

    @Override
    @PutMapping("/deleteEmail/{emailId}")
    public void deleteEmail(@PathVariable String emailId){
        Email email = emailService.getById(emailId);
        email.setIsDelete("1");
        emailService.updateById(email);
    }

    @Override
    @PutMapping("/rebackEmail/{emailId}")
    public void rebackEmail(@PathVariable String emailId){
        Email email = emailService.getById(emailId);
        email.setIsDelete("");
        emailService.updateById(email);
    }
}

